package demogeneric;

/**
 * 1. 写一个泛型类，求一个泛型数组中的最大值
 * 2. 传入的T必须实现Comparable接口（泛型的上界）
 */
class Alg<T extends Comparable<T>> {
    public T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            //if (max < array[i]) {//array[i]为引用类型T，不能用 > < 号进行比较
            if (max.compareTo(array[i]) < 0) {//max < array[i]
                max = array[i];
            }
        }
        return max;
    }
}

class Student implements Comparable<Student> {

    @Override
    public int compareTo(Student o) {
        return 0;
    }
}

public class Test2 {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};
        Alg<Integer> alg = new Alg<>();
        Integer max = alg.findMax(array);
        System.out.println(max);

        Student[] students = new Student[3];
        Alg<Student> alg2 = new Alg<>();//err 该类未实现Comparable接口
        Student max2 = alg2.findMax(students);
        System.out.println(max);
    }
}
